/** 
 * @date 06/05/2013
 * @author Renet_Pourriau
 * @version 1.0
 */

package temporel;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * This class represent date
 */
public class DateC {

	private EnumJourSemaine jourSemaine;

	private Integer numJour;

	private Integer mois;

	private Integer annee;

	/**
	 * this is the constructor of date
	 * @param jourSemaine
	 * @param numJour
	 * @param mois
	 * @param annee
	 */
	public DateC(EnumJourSemaine jourSemaine, Integer numJour, Integer mois,
			Integer annee) {
		if (this.verifDate(numJour, mois, annee)) {
			this.setJourSemaine(jourSemaine);
			this.mois = mois;
			this.annee = annee;
			this.numJour = numJour;
		}
	}

	/**
	 * @return the jourSemaine
	 */
	public EnumJourSemaine getJourSemaine() {
		return jourSemaine;
	}

	/**
	 * @param jourSemaine
	 *            the jourSemaine to set
	 */
	public void setJourSemaine(EnumJourSemaine jourSemaine) {
		this.jourSemaine = jourSemaine;
	}

	/**
	 * @return the numJour
	 */
	public Integer getNumJour() {
		return numJour;
	}

	/**
	 * @param numJour
	 *            the numJour to set
	 */
	public void setNumJour(Integer numJour) {
		if (this.verifDate(numJour, this.getMois(), this.getAnnee())) {
			this.numJour = numJour;
		}
	}

	/**
	 * @return the mois
	 */
	public Integer getMois() {
		return mois;
	}

	/**
	 * @param mois
	 *            the mois to set
	 */
	public void setMois(Integer mois) {
		if (verifDate(this.getNumJour(), mois, this.getAnnee())) {
			this.mois = mois;
		}
	}

	/**
	 * @return the annee
	 */
	public Integer getAnnee() {
		return annee;
	}

	/**
	 * @param annee
	 *            the annee to set
	 */
	public void setAnnee(Integer annee) {
		if (verifDate(this.getNumJour(), this.getMois(), annee)) {
			this.annee = annee;
		}
	}

	/**
	 * @return String representing date in format "jj/mm/aaaa"
	 */
	public String getDate() {
		if (this.getNumJour() == null || this.getMois() == null || this.getAnnee() == null) {
			return null;
		} else {
			String jour = this.getNumJour().toString();
			String mois = this.getMois().toString();
			int lengthJour = this.getNumJour().toString().length();
			if (lengthJour == 1) {
				jour = "0" + this.getNumJour();
			}
			int lengthMois = new Integer(this.getMois().toString().length());
			if (lengthMois == 1) {
				mois = "0" + this.getMois();
			}
			return jour + "/" + mois + "/" + this.getAnnee();
		}
	}

	/**
	 * @return String representing date in french format
	 * @throws IOException 
	 * @throws FileNotFoundException
	 */
	public String getFrenchDate() throws IOException, FileNotFoundException {
		if (this.getJourSemaine() == null || this.getNumJour() == null
				|| this.getMois() == null || this.getAnnee() == null) {
			return null;
		}
		Properties frenchProperty = new Properties();
		InputStream file = new BufferedInputStream(this.getClass().getResourceAsStream(
				"../french.properties"));
		try {
			frenchProperty.load(file);
		} finally {
			file.close();
		}
		Integer mois = new Integer(this.getMois());
		return this.getJourSemaine() + " " + this.getNumJour() + " "
				+ frenchProperty.getProperty(mois.toString()) + " "
				+ this.getAnnee();
	}

	/**
	 * @return String representing date in english format
	 * @throws IOException 
	 * @throws FileNotFoundException
	 */
	public String getEnglishDate() throws IOException, FileNotFoundException {
		if (this.getJourSemaine() == null || this.getNumJour() == null
				|| this.getMois() == null || this.getAnnee() == null) {
			return null;
		}
		Properties englishProperty = new Properties();
		InputStream file = new BufferedInputStream(this.getClass().getResourceAsStream(
				"../english.properties"));
		try {
			englishProperty.load(file);
		} finally {
			file.close();
		}
		String stringJourSemaine = this.getJourSemaine().toString();
		String stringMois = new String(this.getMois().toString());
		String stringNumJour = new String(this.getNumJour().toString());
		return englishProperty.getProperty(stringJourSemaine) + ", "
				+ englishProperty.getProperty(stringMois+"m") + " "
				+ englishProperty.getProperty(stringNumJour) + " "
				+ this.getAnnee();
	}

	/**
	 * @param numJour
	 * @param mois
	 * @param annee
	 * @return true if date exist really
	 */
	public boolean verifDate(Integer numJour, Integer mois, Integer annee) {
		boolean dateCorrect = false;
		if (annee != null && annee > 0 && mois != null && mois > 0
				&& mois <= 12) {
			dateCorrect = true;
			int bisextile = annee % 4;
			switch (mois) {
			case 1:
				if (numJour < 0 || numJour > 31) {
					dateCorrect = false;
				}
				break;
			case 2:
				if (bisextile == 0) {
					if (numJour < 0 || numJour > 29) {
						dateCorrect = false;
					}
				} else {
					if (numJour < 0 || numJour > 28) {
						dateCorrect = false;
					}
				}
				break;
			case 3:
				if (numJour < 0 || numJour > 31) {
					dateCorrect = false;
				}
				break;
			case 4:
				if (numJour < 0 || numJour > 30) {
					dateCorrect = false;
				}
				break;
			case 5:
				if (numJour < 0 || numJour > 31) {
					dateCorrect = false;
				}
				break;
			case 6:
				if (numJour < 0 || numJour > 30) {
					dateCorrect = false;
				}
				break;
			case 7:
				if (numJour < 0 || numJour > 31) {
					dateCorrect = false;
				}
				break;
			case 8:
				if (numJour < 0 || numJour > 31) {
					dateCorrect = false;
				}
				break;
			case 9:
				if (numJour < 0 || numJour > 30) {
					dateCorrect = false;
				}
				break;
			case 10:
				if (numJour < 0 || numJour > 31) {
					dateCorrect = false;
				}
				break;
			case 11:
				if (numJour < 0 || numJour > 30) {
					dateCorrect = false;
				}
				break;
			case 12:
				if (numJour < 0 || numJour > 31) {
					dateCorrect = false;
				}
				break;
			}
		}
		return dateCorrect;
	}
	
	/**
	 * @param date
	 * @return true if current date is before parameter date
	 */
	public boolean estAvant(DateC date) {
		if (this.getDate() != null && date.getDate() != null) {
			if (this.getAnnee() < date.getAnnee()) {
				return true;
			} else if (this.getAnnee().equals(date.getAnnee())) {
				if (this.getMois() < date.getMois()) {
					return true;
				} else if (this.getMois().equals(date.getMois()) && this.getNumJour() < date.getNumJour()) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * @param date
	 * @return true if current date is after parameter date
	 */
	public boolean estApres(DateC date) {
		if (this.getDate() != null && date.getDate() != null) {
			if (this.getAnnee() > date.getAnnee()) {
				return true;
			} else if (this.getAnnee().equals(date.getAnnee())) {
				if (this.getMois() > date.getMois()) {
					return true;
				} else if (this.getMois().equals(date.getMois()) && this.getNumJour() > date.getNumJour()) {
					return true;
				}
			}
		}
		return false;
	}

}